package com.situ.blog.dao.impl;

import com.situ.blog.dao.IBlogDao;
import com.situ.blog.pojo.entity.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.pojo.vo.BlogVo;
import com.situ.blog.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class BlogDaoImpl implements IBlogDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<BlogVo> selectByPage(BlogQuery blogQuery) {
        String sql = "SELECT b.id,b.`title`,b.image,b.click_count,b.content,b.`status`,b.create_time,b.update_time,t.id AS typeId,t.`type` AS typeName\n" +
                "FROM blog AS b INNER JOIN type AS t\n" +
                "ON b.type_id=t.id where 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryTitle = blogQuery.getTitle();
        if (queryTitle != null && !queryTitle.equals("")) {
            sql += " and b.title like ?";
            queryList.add("%" + queryTitle + "%");
        }
        String queryContent = blogQuery.getContent();
        if (queryContent != null && !queryContent.equals("")) {
            sql += " and b.content like ?";
            queryList.add("%" + queryContent + "%");
        }
        String queryTypeId = blogQuery.getTypeId();
        if (!ObjectUtils.isEmpty(queryTypeId)) {
            sql += " and b.type_id=?";
            queryList.add(queryTypeId);
        }
        Date queryBeginCreateTime = blogQuery.getBeginCreateTime();
        Date queryEndCreateTime = blogQuery.getEndCreateTime();
        if (!ObjectUtils.isEmpty(queryBeginCreateTime)&&!ObjectUtils.isEmpty(queryEndCreateTime)) {
            sql += " and create_time between ? and ?";
            queryList.add(queryBeginCreateTime);
            queryList.add(queryEndCreateTime);
        }
        sql += " limit ?,?";
        int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
        queryList.add(offset);
        queryList.add(blogQuery.getLimit());
        List<BlogVo> list = template.query(sql,
                new BeanPropertyRowMapper<>(BlogVo.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectTotalCount(BlogQuery blogQuery) {
        String sql = "SELECT count(*) FROM blog AS b INNER JOIN type AS t\n ON b.type_id=t.id where 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryTitle = blogQuery.getTitle();
        if (queryTitle != null && !queryTitle.equals("")) {
            sql += " and b.title like ?";
            queryList.add("%" + queryTitle + "%");
        }
        String queryContent = blogQuery.getContent();
        if (queryContent != null && !queryContent.equals("")) {
            sql += " and b.content like ?";
            queryList.add("%" + queryTitle + "%");
        }
        String queryTypeId = blogQuery.getTypeId();
        if (!ObjectUtils.isEmpty(queryTypeId)) {
            sql += " and b.type_id=?";
            queryList.add(queryTypeId);
        }
        Date queryBeginCreateTime = blogQuery.getBeginCreateTime();
        Date queryEndCreateTime = blogQuery.getEndCreateTime();
        if (!ObjectUtils.isEmpty(queryBeginCreateTime)&&!ObjectUtils.isEmpty(queryEndCreateTime)) {
            sql += " and create_time between ? and ?";
            queryList.add(queryBeginCreateTime);
            queryList.add(queryEndCreateTime);
        }
        Integer count = template.queryForObject(sql, Integer.class, queryList.toArray());
        return count;
    }

    @Override
    public void add(Blog blog) {
        template.update("insert into blog(title,image,content) values(?,?,?)",
                blog.getTitle(), blog.getImage(), blog.getContent());
    }

    @Override
    public void deleteById(Integer id) {
        template.update("delete from blog where id=?", id);
    }

    @Override
    public Blog selectById(Integer id) {
        return template.queryForObject("select * from blog where id=?",
                new BeanPropertyRowMapper<>(Blog.class), id);
    }

    @Override
    public void update(Blog blog) {
        template.update("update blog set title=?,image=?,content=? where id=?",
                blog.getTitle(), blog.getImage(), blog.getContent(), blog.getId());
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        template.update("update blog set status=? where id=?",
                status, id);
    }
}
